import Taro from '@tarojs/taro'
import app from '../../../main.js'
let myJs = {
    getData: function() {
        this.state = {
            tabIndex: 0,
            eye_template: {},
            case_detail: {
                id: '',
                inspect_data: ''
            },
            inspect_data: {
                'v1': {
                    '裸眼视力': {'v1': '', 'v2': '', 'v3': '', 'v4': ''}, 
                    '矫正视力': {'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '光定位':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '色觉':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '眼睑':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '睫毛':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '眼位':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '眼球':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '结膜':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '泪道冲洗':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '胬肉':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '角膜':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '前房':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '虹膜':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '瞳孔':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '晶状体':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '玻璃体':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '眼底':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '眼压':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    'A超B超':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '拱高':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '内皮细胞数':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '光焦度':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                }, 
                'v2': {
                    '裸眼视力': {'v1': '', 'v2': '', 'v3': '', 'v4': ''}, 
                    '矫正视力': {'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '光定位':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '色觉':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '眼睑':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '睫毛':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '眼位':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '眼球':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '结膜':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '泪道冲洗':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '胬肉':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '角膜':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '前房':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '虹膜':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '瞳孔':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '晶状体':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '玻璃体':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '眼底':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '眼压':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    'A超B超':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '拱高':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '内皮细胞数':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '光焦度':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                }
            }
        };
        return this.state;
    },

    updateCase_detail() {
        var me = this;
        me.state.case_detail.inspect_data = JSON.stringify(me.state.inspect_data);
        app.post("api/case_details/update", me.state.case_detail, function(res) {
            app.navigateBack('../editCase/editCase?id=' + me.state.case_detail.id);
        });
    },
    getCase_detail() {
        var me = this;
        app.get("api/case_details/get", {id: me.state.case_detail.id}, function(res) {
            var inspect_data = {
                'v1': {
                    '裸眼视力': {'v1': '', 'v2': '', 'v3': '', 'v4': ''}, 
                    '矫正视力': {'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '光定位':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '色觉':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '眼睑':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '睫毛':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '眼位':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '眼球':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '结膜':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '泪道冲洗':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '胬肉':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '角膜':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '前房':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '虹膜':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '瞳孔':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '晶状体':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '玻璃体':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '眼底':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '眼压':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    'A超B超':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '拱高':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '内皮细胞数':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '光焦度':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                }, 
                'v2': {
                    '裸眼视力': {'v1': '', 'v2': '', 'v3': '', 'v4': ''}, 
                    '矫正视力': {'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '光定位':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '色觉':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '眼睑':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '睫毛':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '眼位':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '眼球':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '结膜':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '泪道冲洗':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '胬肉':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '角膜':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '前房':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '虹膜':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '瞳孔':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '晶状体':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '玻璃体':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '眼底':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '眼压':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    'A超B超':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '拱高':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '内皮细胞数':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                    '光焦度':{'v1': '', 'v2': '', 'v3': '', 'v4': ''},
                }
            };

            if(res.data.result.inspect_data) {
                inspect_data = JSON.parse(res.data.result.inspect_data);
            }

            var base_template = res.data.result.base_template;
            var eye_template = {};
            if (base_template.length > 0) {
                for (var i = 0; i < base_template.length; i++) {
                    var item = base_template[i];
                    if (item.type == '眼科检查') {
                        eye_template = item;
                        break;
                    }
                }
            }

            console.log(eye_template);

            me.setState({
                case_detail: res.data.result,
                inspect_data: inspect_data,
                eye_template: eye_template
            });
        });
    },
    componentWillMount () {
        this.init();
    },
    navigateTo(url) {
        app.navigateTo(url);
    },
    
    handleDetailChange(name, event) {
        var names = name.split('.');
        var n = names[0];
        var temp = this.state;
        for(var i = 0; i < names.length - 1; i++) {
            temp = temp[names[i]];
        }

        if(names.length > 1) {
            temp[names[i]] = event.detail.value;
            this.setState({n: temp});
        } else {
            this.setState({n: event.detail.value});
        }
    },
    handleChange(name, v) {
        var names = name.split('.');
        var n = names[0];
        var temp = this.state;
        for(var i = 0; i < names.length - 1; i++) {
            temp = temp[names[i]];
        }
        
        temp[names[i]] = v;
        this.setState({n: temp});
    },
    redirectTo(url) {
        app.redirectTo(url);
    },
    componentDidMount () {},
    componentWillUnmount () {},
    componentDidHide () {}
}
export default myJs;
